/**
 * Created by vincent on 2017/2/10.
 */
moduleUtils.directive('cmDatepicker', function () {
    var directive = {};
    directive.restrict = 'A';
    directive.require = '?ngModel';
    directive.scope = {
        model: '=ngModel'
    };

    directive.link = function (scope, element, attrs, ngModel) {
        $(element).datepicker({
            format: 'yyyy-mm-dd',
            container: $(element).parent(),
            orientation: 'bottom left',
            autoclose: true//选中之后自动隐藏日期选择框
        }).on("changeDate", function (e) {
            if (!StringUtils.isNlOrUndOrEmpty(e.date)) {
                scope.$apply(function () {
                    scope.model = e.date.Format("yyyy-MM-dd");
                });
            }
        });
        if (!isNaN(scope.model)) {
            var date = new Date(scope.model);
            //scope.model = date.Format("yyyy-MM-dd hh:mm:ss");
            scope.model = date.Format("yyyy-MM-dd");
            $(element).datepicker("setDate", date);
        }
    };
    return directive;
});